package com.sj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sj.common.lang.ApiResultMsg;
import com.sj.entity.Blog;
import com.sj.entity.Category;
import com.sj.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 分类模块前端控制器
 * </p>
 *
 * @author 小七
 * @since 2021-07-30
 */
@Api(tags = "分类模块")
@RestController
@RequestMapping("/api/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @PutMapping("/save")
    @ApiOperation(value = "添加分类接口")
    public ApiResultMsg<Category> save(@RequestBody @Validated Category category) {
        boolean result = categoryService.save(category);
        if (result) {
            return ApiResultMsg.success("添加成功");
        }
        return ApiResultMsg.error("添加失败");
    }

    @DeleteMapping("/delete/{categoryId}/{userId}")
    @ApiOperation(value = "删除分类接口", notes = "会先判断该分类下是否有博客，没有才会删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "分类id", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, defaultValue = "1")
    })
    public ApiResultMsg<Category> delete(@PathVariable("categoryId") Integer categoryId, @PathVariable("userId") Integer userId) {
        int blogCount = blogService.count(new QueryWrapper<Blog>().eq("category_id", categoryId).eq("user_id", userId));
        if (blogCount > 0) {
            return ApiResultMsg.error("当前分类下有博客不能删除");
        }
        boolean result = categoryService.remove(new QueryWrapper<Category>().eq("id", categoryId).eq("user_id", userId));
        if (result) {
            return ApiResultMsg.success("删除成功");
        }
        return ApiResultMsg.error("删除失败");
    }


    @ApiOperation(value = "更新分类接口")
    @PostMapping("/update")
    public ApiResultMsg<Category> update(@RequestBody @Validated Category category) {
        boolean result = categoryService.updateById(category);
        if (result) {
            return ApiResultMsg.success("更新成功");
        }
        return ApiResultMsg.error("更新失败");
    }

    @GetMapping("/list/{userId}/{current}/{size}")
    @ApiOperation(value = "查询所有分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "current", value = "当前页", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示条数", required = true, defaultValue = "5")
    })
    public ApiResultMsg<Category> listTagByUser(@PathVariable(value = "userId") Integer userId, @PathVariable(value = "current") Integer current, @PathVariable(value = "size") Integer size) {
        IPage<Category> tagPageInfo = categoryService.listCategoryByPage(userId, current, size);
        return ApiResultMsg.success(tagPageInfo);
    }

    @GetMapping("/list/{current}/{size}")
    @ApiOperation(value = "查询所有分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示条数", required = true, defaultValue = "5")
    })
    public ApiResultMsg<Category> listAllTag(@PathVariable(value = "current") Integer current, @PathVariable(value = "size") Integer size) {
        IPage<Category> tagPageInfo = categoryService.listCategoryByPage(null, current, size);
        return ApiResultMsg.success(tagPageInfo);
    }

}

